package com.gateway.filter

import com.yfree.config.YConfig
import com.yfree.global.YController
import org.springframework.cloud.gateway.filter.GatewayFilterChain
import org.springframework.cloud.gateway.filter.GlobalFilter
import org.springframework.core.Ordered
import org.springframework.stereotype.Component
import org.springframework.web.server.ServerWebExchange
import reactor.core.publisher.Mono
import java.nio.charset.StandardCharsets

@Component
class MainFilter : YController(), GlobalFilter, Ordered {
    override fun filter(exchange: ServerWebExchange?, chain: GatewayFilterChain?): Mono<Void>? {
        val request = exchange?.request
        val response = exchange?.response
        yLog(
            """Request:
                Headers: 
                    ${request?.headers}
                Params: 
                    ${request?.queryParams}
            """.trimIndent()
        )
        if (YConfig.Response.isJsonHeader)
            response?.headers?.add("Content-Type", "application/json; charset=${StandardCharsets.UTF_8.name()}")
        return chain?.filter(exchange)
    }

    override fun getOrder(): Int {
        return 0
    }
}
